package com.open4cn.simulator.answer.datacolor;

import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerModeEnum;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.DataColorStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description 绿板校正
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-04 19:23
 */
public class UV100Greenboard600Answer extends AbstractAnswer {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public UV100Greenboard600Answer() {
        super("UV100Greenboard600", AnswerModeEnum.SUCCESS);
    }

    public AnswerResponse answer(DataColorStatus dataColorStatus, String instruction) {
        if (!AnswerModeEnum.FAIL.equals(getAnswerMode())) {
            getActions().forEach(action -> {
                action.action(dataColorStatus, instruction);
            });
        }
        AnswerResponse response = getAnswer(dataColorStatus);
        return response;
    }

    /**
     * 获取应答结果
     *
     * @param dataColorStatus
     * @return
     */
    private AnswerResponse getAnswer(DataColorStatus dataColorStatus) {
        switch (getAnswerMode()) {
            case SUCCESS:
                return AnswerResponse.success(success(dataColorStatus));
            case FAIL:
                return AnswerResponse.fail(fail(dataColorStatus));
            case RANDOM:
                return AnswerResponse.success(random(dataColorStatus));
            case LOOP:
                return AnswerResponse.success(getLoop());
            case INPUT:
                Object input = pollInput();
                if (null != input) {
                    return AnswerResponse.success(input);
                }
            default:
                return AnswerResponse.success(success(dataColorStatus));
        }
    }


    /**
     * 成功应答
     * *INR000xxxx1xxxxs9.22
     * 038.233,041.364,043.910,045.604,046.835,
     * 047.870,048.842,049.644,050.739,052.383,
     * 054.941,058.259,062.759,067.369,072.210,
     * 075.640,076.751,076.249,074.702,072.501,
     * 070.122,067.873,065.813,064.048,062.535,
     * 061.436,060.467,059.769,059.511,059.618,
     * 059.741,059.732,060.497,061.290,062.314,
     * 000.000,000.000,000.000,000.000,000.000,
     * 45AF:
     *
     * @param dataColorStatus
     * @return
     */
    private Object success(DataColorStatus dataColorStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("*")
                .append(dataColorStatus.getCurrentSpecularGloss().getCode())
                .append(dataColorStatus.getCurrentAperture().name())
                .append("R")
                .append(dataColorStatus.getCurrentUV().getCode())
                .append("xxxx1xxxxs9.22")
                .append("\r\n");
        builder.append("038.233,041.364,043.910,045.604,046.835,").append("\r\n");
        builder.append("047.870,048.842,049.644,050.739,052.383,").append("\r\n");
        builder.append("054.941,058.259,062.759,067.369,072.210,").append("\r\n");
        builder.append("075.640,076.751,076.249,074.702,072.501,").append("\r\n");
        builder.append("070.122,067.873,065.813,064.048,062.535,").append("\r\n");
        builder.append("061.436,060.467,059.769,059.511,059.618,").append("\r\n");
        builder.append("059.741,059.732,060.497,061.290,062.314,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("45AF:").append("\r\n");
        return builder.toString();
    }


    /**
     * 失败应答
     * *IAR000xxEx1xxxxs9.22
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 000.000,000.000,000.000,000.000,000.000,
     * 45AF:
     *
     * @param dataColorStatus
     * @return
     */
    private Object fail(DataColorStatus dataColorStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("*")
                .append(dataColorStatus.getCurrentSpecularGloss().getCode())
                .append(dataColorStatus.getCurrentAperture().name())
                .append("R")
                .append(dataColorStatus.getCurrentUV().getCode())
                .append("xxEx1xxxxs9.22")
                .append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("000.000,000.000,000.000,000.000,000.000,").append("\r\n");
        builder.append("45AF:").append("\r\n");
        return builder.toString();
    }


    /**
     * 随机反射率
     *
     * @param dataColorStatus
     * @return
     */
    private Object random(DataColorStatus dataColorStatus) {
        return success(dataColorStatus);
    }

}
